package com.bookcode.controller;

import com.bookcode.dao.BookDao;
import com.bookcode.entity.Book;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookDao bookDao;
    //查询所有图书
    @RequestMapping("/list")
    public ModelAndView list(){
        ModelAndView mav=new ModelAndView();
        mav.addObject("booklist", bookDao.findAll());
        mav.setViewName("bookList");
        return mav;
    }
    //添加图书
    @RequestMapping(value="/add",method=RequestMethod.POST)
    public String add(Book book){
        bookDao.save(book);
        return "forward:/book/list";
    }
    //根据id查询book实体
    @RequestMapping("/preUpdate/{id}")
    public ModelAndView preUpdate(@PathVariable("id")Integer id){
        ModelAndView mav=new ModelAndView();
        mav.addObject("book", bookDao.getOne(id));
        mav.setViewName("bookUpdate");
        return mav;
    }
    //修改图书
    @PostMapping(value="/update")
    public String update(Book book){
        bookDao.save(book);
        return "forward:/book/list";
    }
    //删除图书
    @GetMapping("/delete")
    public String delete(Integer id){
        bookDao.deleteById(id);
        return "forward:/book/list";
    }
    //根据条件动态查询
    @RequestMapping("/list2")
    public ModelAndView list2(Book book){
        ModelAndView mav = new ModelAndView();
        List<Book> bookList = bookDao.findAll(new Specification<Book>(){
            @Override
            public Predicate toPredicate(Root<Book> root, CriteriaQuery<?> query, CriteriaBuilder cb)
            {
                Predicate predicate = cb.conjunction();
                if(book!=null){
                    if(book.getName()!=null && !"".equals(book.getName())){
                        predicate.getExpressions().add(cb.like(root.get("name"), "%"+book.getName()+"%"));
                    }
                    if(book.getAuthor()!=null && !"".equals(book.getAuthor())){
                        predicate.getExpressions().add(cb.like(root.get("author"), "%"+book.getAuthor()+"%"));
                    }
                }
                return predicate;
            }
        });
        mav.addObject("book", book);
        mav.addObject("booklist", bookList);
        mav.setViewName("booklist");
        return mav;
    }
    //查询
    @ResponseBody
    @RequestMapping("/query")
    public List<Book> findByName(String name){
        return  bookDao.findByName("思想");
    }
    //随机显示
    @ResponseBody
    @RequestMapping("/randomlist")
    public List<Book> randomList(String name){
        return  bookDao.randomList(2);
    }
}
